package com.auction.system.action.auction;


import com.auction.system.base.BaseResponse;
import com.auction.system.domain.dto.auction.AuctionGood;
import com.auction.system.domain.dto.auction.BidRecord;
import com.auction.system.domain.query.auction.BidRecordQuery;
import com.auction.system.enums.EnumAuctionStatus;
import com.auction.system.enums.EnumDateStyle;
import com.auction.system.enums.EnumReturnResult;
import com.auction.system.service.auction.IAuctionGoodService;
import com.auction.system.service.auction.IBidRecordService;
import com.auction.system.utils.DateUtil;
import com.auction.system.utils.StringUtil;
import com.auction.system.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * @ClassName BidRecordAction
 * @Author 王伟学
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/bidRecord")
public class BidRecordAction {

    @Autowired
    private IBidRecordService bidRecordService;

    @Autowired
    private IAuctionGoodService auctionGoodService;


    /**
     * 进入竞价页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView preAdd(Long id){
        ModelAndView mav = new ModelAndView();
        AuctionGood auctionGood = auctionGoodService.get(id);
        mav.addObject("auctionGood",auctionGood);
        mav.setViewName("auctionGood/bidRecord");
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated BidRecord bidRecord, BindingResult result){
        BaseResponse response = new BaseResponse();
        try{
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("BidRecord-add参数有误");
                log.info("BidRecord-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            //判断关联的拍品是否存在
            if(StringUtil.isNull(bidRecord.getGoodId()) || StringUtil.isNull(auctionGoodService.get(bidRecord.getGoodId()))){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("竞价的拍品不存在");
                return response;
            }
            AuctionGood auctionGood = auctionGoodService.get(bidRecord.getGoodId());
            //判断当前竞拍价格是否大于最高竞拍价格
            if((auctionGood.getMaxPrice().longValue() == 0L? auctionGood.getStartPrice():auctionGood.getMaxPrice()).compareTo(bidRecord.getBidPrice()) > 0){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("竞价的价格低于该拍品最高竞价:"+(auctionGood.getMaxPrice().longValue() == 0L? auctionGood.getStartPrice():auctionGood.getMaxPrice())+"元");
                return response;
            }
            //将拍品最高竞价和最高竞价人id更新
            auctionGood.setBidNum((StringUtil.isNull(auctionGood.getBidNum())?0:auctionGood.getBidNum())+1);
            auctionGood.setMaxPrice(bidRecord.getBidPrice());
            auctionGood.setMaxPriceUserId(Long.parseLong(UserUtil.getUserId()));
            auctionGoodService.edit(auctionGood);
            //新增竞价记录
            bidRecord.setBidUserId(Long.parseLong(UserUtil.getUserId()));
            bidRecordService.add(bidRecord);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("竞价成功");
        }catch (Exception e){
            log.error("新增竞价信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("竞价失败");
        }
        return response;
    }

    /**
     * 获取竞价记录信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getBuyList",method = RequestMethod.POST)
    public BidRecordQuery getBuyList(BidRecordQuery query) {
        try {
            query.setBidUserId(Long.parseLong(UserUtil.getUserId()));
            query = bidRecordService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取竞价记录信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取竞价记录信息数据异常");
        }
        return query;
    }

    /**
     * 获取竞价记录信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public BidRecordQuery getList(BidRecordQuery query) {
        try {
            query = bidRecordService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取竞价记录信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取竞价记录信息数据异常");
        }
        return query;
    }
}
